package com.qjquestion.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qjquestion.domain.Customer;
import com.qjquestion.dto.customer.CustomerDto;
import com.qjquestion.mapper.CustomerMapper;
import com.qjquestion.service.CustomerService;
import com.qjquestion.vo.customer.CustomerVo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    @Resource
    private CustomerMapper customerMapper;

    /**
     * 新增会员
     * @param dto 会员信息
     * @return
     */
    @Transactional
    public CustomerVo createCustomer(CustomerDto dto){

        LambdaQueryWrapper<Customer> customerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        customerLambdaQueryWrapper.eq(Customer::getOpenId,dto.OpenId);
        Customer customerEntity = customerMapper.selectOne(customerLambdaQueryWrapper);

        CustomerVo customerVo = new CustomerVo();

        if(Objects.isNull(customerEntity)){
            Customer customer = new Customer();
            BeanUtils.copyProperties(dto,customer);
            customer.setName("题海领航员");
            customerMapper.insert(customer);
            BeanUtils.copyProperties(customer,customerVo);
        }else{
            BeanUtils.copyProperties(customerEntity,customerVo);
        }
        return customerVo;
    }
}
